//
//  main.m
//  OC04_NSString
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {

#pragma mark - 知识点1 不可变字符串
    /** Class: NSString */
        
#pragma mark ** 创建对象相关API
#if 0
        /** 字面量方式, 创建字符串对象, 是常用方法 */
        NSString *str2 = @"iphone";
        NSLog(@"str2:%@", str2);
        
        NSString *str = [[NSString alloc] initWithString:str2];
        NSLog(@"str:%@", str);
        
        /** 带format参数方法, 比较灵活(万能方法), 可以讲任意内容以格式化方式生成字符串对象 */
        NSString *str3 = [[NSString alloc] initWithFormat:@"新建字符串%@", str];
        NSLog(@"str3:%@", str3);
        
        /** 整型a 转化为字符串 */
        int a = 1314;
        NSString *str4 = [[NSString alloc] initWithFormat:@"%d", a];
        NSLog(@"str4:%@", str4);
        
        NSString *str5 = [NSString stringWithString:str4];
        NSLog(@"str5:%@", str5);
        
        NSString *str6 = [NSString stringWithFormat:@"%@", str5];
        NSLog(@"str6:%@", str6);
#endif
#pragma mark ** 直接获取字符串长度 API
#if 0
        NSString *str1 = @"iphone";
        NSLog(@"length:%ld", str1.length);
        
        
#pragma mark ** 直接获取某一个字符 API
        
        /** 取出 h 字符 */
        unichar c = [str1 characterAtIndex:2];
        NSLog(@"%c", c);

#pragma mark ** 直接判断两个字符串是否相同 API
        
        /** !!! equal 相等!!! */
        
        NSString *str1 = @"iphone";
        NSString *str2 = @"iPhone";
        
        if ([str1 isEqualToString:str2]) {
            NSLog(@"用户名正确");
        } else {
            NSLog(@"用户名错误");
        }

#pragma mark ** 直接比较两个字符串 API
        
        NSString *str1 = @"王强";
        NSString *str2 = @"王艳";
        
        [str1 compare:str2];
        NSLog(@"%ld", [str1 compare:str2]);

#pragma mark ** 直接获取一个字符串中某个部分(子字符串)相关 API
        
        /** !!! substring 子字符串!!! */
        
        NSString *str1 = @"I have an iphone10";
        
        /** 取出 I */
        NSString *subStr = [str1 substringToIndex:1];
        NSLog(@"subStr:%@",subStr);
        
        /** 取出 iphone10 */
        NSString *subStr2 = [str1 substringFromIndex:10];
        NSLog(@"subStr:%@", subStr2);
        
        /** 取出have */
        NSRange range = {2, 4};
        NSString *subStr3 = [str1 substringWithRange:range];
        NSLog(@"%@", subStr3);
        
        /** 取出 an */
        NSRange range2 = [str1 rangeOfString:@"an"];
        NSString *subStr4 = [str1 substringWithRange:range2];
        NSLog(@"%@", subStr4);

        
#pragma mark ** 直接对字符串进行拼接相关 API
        
        /** !!! appending 拼接!!! */
        
        /** 拼接 */
        NSString *str1 = @"iphone";
        NSString *str2 = @"10s";
        
        NSString *str3 = [str1 stringByAppendingString:str2];
        NSLog(@"%@", str3);
        
        NSString *str4 = [str1 stringByAppendingFormat:@" %@",str2];
        NSLog(@"%@", str4);

        
#pragma mark ** 将一个字符串中的某个子字符串直接替换成其他字符串
        
        /** !!! replacing 替换!!! */
        
        NSString *str1 = @"I have an iphone";
        
        /** 需求: 将iphone 换成 xiaomi */
        NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"iphone" withString:@"xiaomi"];
        NSLog(@"str2:%@", str2);
        
        NSRange range2 = [str1 rangeOfString:@"iphone"];
        NSString *str3 = [str1 stringByReplacingCharactersInRange:range2 withString:@"0.0"];
        NSLog(@"%@", str3);
        
        NSString *str4 = [str1 stringByReplacingCharactersInRange:[str1 rangeOfString:@"iphone"] withString:@"!!"];
        NSLog(@"%@", str4);

  
#pragma mark ** 字符串@"23412"直接转换为int 类型 API
        
        NSString *str1 = @"12345";
        int a = str1.intValue;
        NSLog(@"a = %d", a);
        
#pragma mark ** 直接对字符串字母进行大小写的相关转换 API(case)
        
        NSString *str2 = @"I have an iphone10";
        
        /** 字符全变成大写 */
        NSString *str3 = [str2 uppercaseString];
        NSLog(@"%@", str3);
        
        /** 字符全变成小写 */
        NSString *str4 = str2.lowercaseString;
        NSLog(@"%@", str4);
        
        /** 字符串,每个首字母大写 */
        NSString *str5 = str2.capitalizedString;
        NSLog(@"%@", str5);
        
        
        /** 需求: 将姓名的首字母提取出来, 并且转化为大写字母. */
        NSString *name = @"wangqiang";
        NSString *first = [name substringToIndex:1].capitalizedString;
        NSLog(@"%@", first);
 
        
#pragma mark ** 字符串是否以 xx 开头或结尾
        
        NSString *str1 = @"http://www.kengni.com";
        
        if ([str1 hasPrefix:@"http"]) {
            NSLog(@"字符串是以http开头");
        }
        
        if ([str1 hasSuffix:@".com"]) {
            NSLog(@"字符串是以.com结尾的");
        }

        
        /** http://www.lanou3g.com/icon.png
         判断上述字符串中是否以“png”结尾，如果是就替换成“jpg”，如果不是，就拼接”.jpg”。 */
        
        NSString *str2 = @"http://www.lanou3g.com/icon.png";
        if ([str2 hasSuffix:@"png"]) {
            NSLog(@"%@", [str2 stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"]);
        } else {
            NSLog(@"%@", [str2 stringByAppendingString:@".jpg"]);
        }
        
 #endif
     
#pragma mark - 知识点2 可变字符串
        
        /** 是NSString的子类, 继承了NSString 所有属性和方法
         *
         * 可变意味着自身内容和长度是动态的(可以变化的)
         */
        
#pragma mark ** 创建对象相关API
#if 0
        NSMutableString *mStr1 = [[NSMutableString alloc] initWithString:@"I have an iphone"];
        
        NSMutableString *mStr2 = [NSMutableString stringWithString:@"iOS"];
        
        NSMutableString *mStr3 = [NSMutableString stringWithFormat:@"%@", mStr1];

#pragma mark ** 拼接字符串 API
        NSMutableString *mStr1 = [NSMutableString stringWithString:@"iphone"];
        /** 需求: "iphone" 后面加上 "6Plus" */
        [mStr1 appendString:@"6Plus"];
        NSLog(@"%@", mStr1);

        
#pragma mark ** 删除一个范围内的字符 API
        
        NSMutableString *mStr1 = [NSMutableString stringWithString:@"http://www.kengni.com"];
        
        /** 需求: 删除 "http://" */
        
        [mStr1 deleteCharactersInRange:[mStr1 rangeOfString:@"http://"]];
        NSLog(@"%@", mStr1);

        
#pragma mark ** 字符串中插入相应的字符串 API
        
        NSMutableString *mStr1 = [NSMutableString stringWithString:@"http://www.kengni.com"];
        
        /** 需求: 在http后面加上一个s. 即https://www.kengni.com */
        
        
        [mStr1 insertString:@"s" atIndex:4];
        NSLog(@"%@", mStr1);
        
        NSRange range = [mStr1 rangeOfString:@"kengni"];
        [mStr1 insertString:@"?" atIndex:range.location + range.length];
        NSLog(@"%@", mStr1);
  
       
#pragma mark ** 字符串替换 API
        NSMutableString *mStr1 = [NSMutableString stringWithString:@"I have an iphone10"];
        
        [mStr1 replaceCharactersInRange:[mStr1 rangeOfString:@"iphone"] withString:@"xiaomi"];
        NSLog(@"%@", mStr1);

        
#pragma mark ** 重置字符串 API
        
        NSMutableString *mStr1 = [NSMutableString stringWithString:@"iOS"];
        [mStr1 setString:@"iphone"];
        NSLog(@"%@", mStr1);
        
#endif
        
#pragma mark - 知识点3 NSNumber
        
        /** class: NSNumber
         * 此类的作用: 基本数据类型和OC对象进行相互转换
         *
         * 详见 API
         */
        
        /** 创建对象 */
        NSNumber *num1 = [NSNumber numberWithInt:38];
        NSLog(@"%@", num1);
        
        int a = 100;
        NSNumber *num2 = [NSNumber numberWithInt:a];
        NSLog(@"%@", num2);
        
        /** 字面量创建 */
        NSNumber *num3 = @20;
        
        int b = 200;
        NSNumber *num4 = @(b);
        
        
        /** 从NSNumber对象转化为基本数据类型 */
        int aa = num1.intValue;
        NSLog(@"aa:%d", aa);
        
        /** NSNumber对象之间比较 */
        [num1 compare:num2];
        NSLog(@"%ld", [num1 compare:num2]);
        
#pragma mark - 知识点4 NSValue
        
        /** 
         * calss: NSValue
         * 将结构体和对象类型进行互相转化
         */
        NSRange range = {3, 4};
        
        /** 创建对象 */
        NSValue *value = [NSValue valueWithRange:range];
        NSLog(@"%@", value);
        
        
        /** 转换为结构体 */
        NSRange newRange = value.rangeValue;
        NSLog(@"%ld, %ld", newRange.location, newRange.length);
        
        
        
        
  
        
        
        
        
        
        
       
       
        
       
        
        
       
       
        
        
        
        
        
        
        
        
        
        
        
        
    }
    return 0;
}
